/*
 * TODO: License
 */
package com.rppowell.pixie.swing.map;

import com.rppowell.pixie.models.MapProperties;
import com.rppowell.pixie.models.SpriteSet;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author rppowell
 */
public class MapPropertiesPanel extends JPanel {
    private SpriteSet spriteSet;
    private JTextField name;
    private JSpinner wSpinner;
    private JSpinner hSpinner;
    
    public MapPropertiesPanel(SpriteSet s) {
        spriteSet = s;
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        
        JLabel label;
        this.setLayout(gridbag);
        
        c.fill = GridBagConstraints.BOTH;
        c.gridx=0;
        c.gridy=0;
        label = new JLabel("Map Name:");
        gridbag.setConstraints(label, c);
        add(label);
        
        c.gridx=1;
        c.gridy=0;
        name = new JTextField();
        gridbag.setConstraints(name, c);
        add(name);
        
        
        c.gridx=0;
        c.gridy=1;
        label = new JLabel("Width:");
        gridbag.setConstraints(label, c);
        add(label);
        
        c.gridx=1;
        c.gridy=1;
        wSpinner = new JSpinner(new SpinnerNumberModel(16, 1, 255,1));
        gridbag.setConstraints(wSpinner, c);
        add(wSpinner);
        
        
        c.gridx=0;
        c.gridy=2;
        label = new JLabel("Height:");
        gridbag.setConstraints(label, c);
        add(label);
        
        c.gridx=1;
        c.gridy=2;
        hSpinner = new JSpinner(new SpinnerNumberModel(16, 1, 255,1));
        gridbag.setConstraints(hSpinner, c);
        add(hSpinner);
    }
    
    public void setMapProperties(MapProperties properties) {
        if (properties != null) {
            spriteSet = properties.getSpriteSet();
            name.setText(properties.getMapName());
            
            SpinnerModel m;
            
            m = wSpinner.getModel();
            if (m instanceof SpinnerNumberModel) {
                m.setValue(properties.getMapWidth());
            }
            m = hSpinner.getModel();
            if (m instanceof SpinnerNumberModel) {
                m.setValue(properties.getMapHeight());
            }
        }
    }
    
    public MapProperties getMapProperties() {
        MapProperties properties = null;
                
        Object wo = wSpinner.getModel().getValue();
        int w = Integer.parseInt(wo.toString());

        Object ho = hSpinner.getModel().getValue();
        int h = Integer.parseInt(wo.toString());
        
        if (w > 0 && h > 0) {
            properties = new MapProperties(spriteSet, w, h, name.getText());
        }
        return properties;
    }
}
